/**************************************************************
// 特征模型仿真例2：非线性系统控制
simucpp版本：2.1.13
**************************************************************/
#include <cmath>
#include "env.hpp"

Plant::Plant() {
    intx1 = new simucpp::UIntegrator(&sim1, "intx1");
    intx2 = new simucpp::UIntegrator(&sim1, "intx2");
    intx3 = new simucpp::UIntegrator(&sim1, "intx3");
    fcnx1 = new simucpp::UFcnMISO(&sim1, "fcnx1");
    fcnx2 = new simucpp::UFcnMISO(&sim1, "fcnx2");
    in1 = new simucpp::UGain(&sim1, "in1");
    sim1.connectU(intx1, fcnx1);
    sim1.connectU(intx2, fcnx1);
    sim1.connectU(in1,   fcnx1);
    sim1.connectU(intx1, fcnx2);
    sim1.connectU(intx2, fcnx2);
    sim1.connectU(in1,   fcnx2);
    sim1.connectU(fcnx1, intx1);
    sim1.connectU(fcnx2, intx2);
    sim1.connectU(fcnx2, intx2);
    sim1.connectU(intx2, intx3);
    intx2->Set_InitialValue(1);
    fcnx1->Set_Function([](double *u){
        double x1 = u[0], x2 = u[1], u1 = u[2];
        return 2*exp(x2)*u1 - x1;
    });
    fcnx2->Set_Function([](double *u){
        double x1 = u[0], x2 = u[1], u1 = u[2];
        return 2*x1*x2 + u1;
    });
}
void Plant::Simulate(uint32_t step) {
    for (uint32_t i = 0; i < step; i++)
        sim1.Simulate_OneStep();    
}
double Plant::Get_Output() {
    return intx3->Get_OutValue();
}
